package edu.nf.takeoutdemo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.nf.takeoutdemo.commons.constant.PasswordConstant;
import edu.nf.takeoutdemo.commons.constant.StatusConstant;
import edu.nf.takeoutdemo.commons.context.BaseContext;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.mapper.EmployeeMapper;
import edu.nf.takeoutdemo.model.converter.EmployeeConverter;
import edu.nf.takeoutdemo.model.dto.EmployeeDTO;
import edu.nf.takeoutdemo.model.dto.EmployeeLoginDTO;
import edu.nf.takeoutdemo.model.dto.EmployeePageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Employee;
import edu.nf.takeoutdemo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/23
 */
@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {

    private final EmployeeMapper employeeMapper;

    private final EmployeeConverter employeeConverter;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        Employee employee = employeeMapper.getByUsername(username);
        if (employee == null) {
            throw new RuntimeException("用户不存在");
        }
        //spring自带的MD5加密
        //对前端传过来的密码进行MD5加密处理，对数据库密码进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            throw  new RuntimeException("密码错误");
        }
        if(employee.getStatus() == StatusConstant.DISABLE){
            throw new RuntimeException("账号已停用");
        }
        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = employeeConverter.toEmployee(employeeDTO);
        employee.setStatus(StatusConstant.ENABLE);
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //设置当前记录的创建时间和修改时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());

        //设置当前记录的创建认id和修改人id
        //employee.setCreateUser(BaseContext.getCurrentId());
        //employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.add(employee);

    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPageNum(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee = new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeMapper.update(employee);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("****");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = employeeConverter.toEmployee(employeeDTO);
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.update(employee);
    }
}
